<%@ tag import="com.kyub.spring.gears.vo.NavigableCollection" %>
<%@ tag body-content="empty" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="spring-sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="cf" uri="http://kyub.com/jsp/jstl/functions" %>
<%@ attribute name="pojoDescriptor" required="true" type="com.kyub.spring.gears.model.PojoDescriptor" %>
<%@ attribute name="pojoList" required="true" type="com.kyub.spring.gears.vo.NavigableCollection" %>
<%@ attribute name="gears_restfull_prefix" required="true" %>

<!--
<h2><spring:message code="${pojoDescriptor.name}.labelList" text="${pojoDescriptor.name}" htmlEscape="true"/></h2>
-->

<spring-sec:authorize ifAnyGranted="${pojoDescriptor.allowedRoles}">
    <spring:message code="deleteConfirmationLabel" var="deleteConfirmationLabelVar" text="'Sure ?'"/>
    


    <div class="ui-corner-all">
    <table >

        <tr>
            <c:forEach items="${pojoDescriptor.listFields}" var="p">
                <th>
                    <c:choose>
                        <c:when test="${!cf:isStructProperty(p) or (cf:isStructProperty(p) and p.layout.label == null)}">
                            <spring:message var="colTitle" code="${pojoDescriptor.name}.${p.name}.labelList"
                                            text="${p.name}"
                                            htmlEscape="true"/>
                        </c:when>
                        <c:otherwise>
                            <c:set var="colTitle" value=""/>
                        </c:otherwise>
                    </c:choose>
                    <c:out value="${colTitle}"/>
                </th>
            </c:forEach>
            <th colspan="2"></th>
        </tr>
        <c:forEach items="${pojoList}" var="pojo">
            <tr>


                <c:forEach items="${pojoDescriptor.listFields}" var="p">

                    <spring-sec:authorize ifAnyGranted="${p.allowedRoles}">

                        <td>
                            <c:choose>
                                <c:when test="${cf:isSimpleProperty(p)}">
                                    <c:choose>
                                        <c:when test="${cf:readProperty(pojo,p.name).class.simpleName == 'Date'}">
                                            <fmt:formatDate value="${cf:readProperty(pojo,p.name)}"/>
                                        </c:when>
                                        <c:when test="${cf:readProperty(pojo,p.name).class.simpleName == 'Boolean'}" >
                                           <c:choose>
                                            <c:when test="${cf:readProperty(pojo,p.name)}">
                                                <span class="ui-icon ui-icon-check"></span>
                                           </c:when>
                                            <c:otherwise>
                                                <span class="ui-icon ui-icon-closethick"></span>
                                            </c:otherwise>
                                            </c:choose>

                                        </c:when>
                                        <c:otherwise>
                                            ${cf:readProperty(pojo,p.name)}
                                        </c:otherwise>
                                    </c:choose>

                                </c:when>
                                <c:when test="${cf:isRelation(p)}">
                                    <c:if test="${not empty p.pojoDescr}">
                                        <a     onclick="openDisplayDialog('${pageContext.request.contextPath}/${gears_restfull_prefix}/${p.pojoDescr.name}/riaview/${cf:readProperty(cf:readProperty(pojo,p.name),p.pojoDescr.idProperty)}',300,300)"
                                                href="#"
                                           > ${cf:readProperty(pojo,p.name)}</a>
                                    </c:if>
                                    <c:if test="${empty p.pojoDescr}">
                                        ${cf:readProperty(pojo,p.name)}
                                    </c:if>
                                </c:when>
                                <c:when test="${cf:isCollection(p)}">
                                    <a      onclick="openDisplayDialog('${pageContext.request.contextPath}/${gears_restfull_prefix}/${pojoDescriptor.name}/riaviewrelated/${cf:readProperty(pojo,pojoDescriptor.idProperty)}/${p.name}',300,300)"
                                            href="#"
                                      >${p.name} </a>
                                </c:when>
                                <c:when test="${cf:isStructProperty(p)}">
                                    <!--
                                    ${p.layout.hrefPattern != null}  ${p.layout.label != null}
                                    -->
                                    <c:choose>
                                        <c:when test="${p.layout.hrefPattern != null}">

                                            <a href="${cf:buildHref(p.layout, pojo,pageContext.request.contextPath)}">
                                                <c:choose>
                                                    <c:when test="${p.layout.label != null}">
                                                        <spring:message code="${pojoDescriptor.name}.${p.layout.label}"
                                                                        text="${p.layout.label}" htmlEscape="false"/>
                                                    </c:when>
                                                    <c:otherwise>
                                                        ${cf:readProperty(pojo,p.property.name)}
                                                    </c:otherwise>
                                                </c:choose>
                                            </a>
                                        </c:when>

                                        <c:otherwise>
                                            <c:choose>
                                                <c:when test="${p.layout.label != null}">
                                                    <spring:message code="${pojoDescriptor.name}.${p.layout.label}"
                                                                    text="${p.layout.label}"
                                                                    htmlEscape="false"/>
                                                </c:when>
                                                <c:otherwise>
                                                    ${cf:readProperty(pojo,p.property.name)}
                                                </c:otherwise>
                                            </c:choose>
                                        </c:otherwise>

                                    </c:choose>


                                </c:when>
                                <c:otherwise>
                                    ${p} ${p.class}
                                </c:otherwise>
                            </c:choose>

                        </td>
                    </spring-sec:authorize>

                </c:forEach>

                <spring-sec:authorize ifAnyGranted="${pojoDescriptor.editableRoles}">
                    <td>
                        <a href="${pageContext.request.contextPath}/${gears_restfull_prefix}/${pojoDescriptor.name}/view/${cf:readProperty(pojo,pojoDescriptor.idProperty)}">view</a>
                    </td>
                </spring-sec:authorize>

                <spring-sec:authorize ifAnyGranted="${pojoDescriptor.editableRoles}">
                    <c:if test="${pojoDescriptor.editable}">
                        <td>
                            <a onclick="openFormDialog('${pageContext.request.contextPath}/${gears_restfull_prefix}/${pojoDescriptor.name}/riaupdate/${cf:readProperty(pojo,pojoDescriptor.idProperty)}')"
                               href="#">edit</a>
                        </td>
                    </c:if>
                </spring-sec:authorize>

                <spring-sec:authorize ifAnyGranted="${pojoDescriptor.deletableRoles}">
                    <c:if test="${pojoDescriptor.deletable}">
                        <td>
                            <a href="${pageContext.request.contextPath}/${gears_restfull_prefix}/${pojoDescriptor.name}/delete/${cf:readProperty(pojo,pojoDescriptor.idProperty)}"
                               onclick="return confirm(${deleteConfirmationLabelVar})">delete</a>
                        </td>
                    </c:if>
                </spring-sec:authorize>

            </tr>
        </c:forEach>
    </table>
    </div>
    <div id="navigator" class="ui-corner-all">
        

        <%
            Integer pages = ((NavigableCollection)request.getAttribute("pojoList")).getPages() ;
            if(pages > 1){
                for(int i = 1; i<= pages; i++ ){
                request.setAttribute("i",i);
       %>
              <a href="#" onclick="shiftPage(${i})"> [ ${i} ]</a>
        <%
                }
            }
        %>
    </div>


</spring-sec:authorize>